package com.huashi.dealer.modular.controller.store.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huashi.dealer.commonEntity.User;
import com.huashi.dealer.commonEntity.UserAddress;
import com.huashi.dealer.core.page.PageFactory;
import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.balance.bo.BalanceBo;
import com.huashi.dealer.modular.business.balance.service.BalanceService;
import com.huashi.dealer.modular.business.balance.vo.BalanceVo;
import com.huashi.dealer.modular.business.user.param.StoreUserListParam;
import com.huashi.dealer.modular.business.user.param.StoreUserSaveParam;
import com.huashi.dealer.modular.business.user.param.StoreUserRechargeParam;
import com.huashi.dealer.modular.business.user.param.StoreUserRechargeApplyParam;
import com.huashi.dealer.modular.business.user.param.StoreUserSalesmanParam;
import com.huashi.dealer.modular.business.user.param.StoreUserAddressListParam;
import com.huashi.dealer.modular.business.user.param.StoreUserAddressSaveParam;
import com.huashi.dealer.modular.business.user.service.StoreUserManageService;
import com.huashi.dealer.modular.business.user.vo.StoreUserListVo;
import com.huashi.dealer.modular.business.user.vo.StoreUserDetailVo;
import com.huashi.dealer.modular.business.user.vo.StoreUserAddressListVo;
import com.huashi.dealer.modular.business.user.vo.StoreUserAddressDetailVo;
import com.huashi.dealer.modular.business.user.vo.StoreUserSalesmanVo;

import java.util.List;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Store端用户管理控制器
 * 对应PHP项目的/store/user功能
 *
 * @author 谭陈强
 * @since 2025-08-07
 */
@Slf4j
@Tag(name = "Store端用户管理控制器")
@RestController("storeUserManageController")
@RequestMapping("/store/storeUser")
@Validated
public class UserManageController {

    @Resource
    private StoreUserManageService storeUserManageService;

    @Resource
    private BalanceService balanceService;
    /**
     * 用户列表
     */
    @GetMapping("/index")
    @Operation(summary = "用户列表")
    public CommonResult<Page<StoreUserListVo>> getUserList(StoreUserListParam param) {
        Page<User> page = PageFactory.defaultPage();
        try {
            Page<StoreUserListVo> userList = storeUserManageService.getUserList(param, page);
            return CommonResult.data(userList);
        } catch (Exception e) {
            log.error("获取用户列表失败", e);
            return CommonResult.error("获取用户列表失败：" + e.getMessage());
        }
    }

    /**
     * 用户详情
     */
    @GetMapping("/detail/{userId}")
    @Operation(summary = "用户详情")
    public CommonResult<StoreUserDetailVo> getUserDetail(@PathVariable Long userId) {
        try {
            StoreUserDetailVo result = storeUserManageService.getUserDetail(userId);
            return CommonResult.data(result);
        } catch (Exception e) {
            log.error("获取用户详情失败", e);
            return CommonResult.error("获取用户详情失败：" + e.getMessage());
        }
    }

    /**
     * 新增用户
     */
    @PostMapping("/add")
    @Operation(summary = "新增用户")
    public CommonResult<String> addUser(@RequestBody StoreUserSaveParam param) {
        try {
            storeUserManageService.addUser(param);
            return CommonResult.ok("添加成功");
        } catch (Exception e) {
            log.error("新增用户失败", e);
            return CommonResult.error("添加失败：" + e.getMessage());
        }
    }

    /**
     * 编辑用户
     */
    @PutMapping("/edit")
    @Operation(summary = "编辑用户")
    public CommonResult<String> editUser(@RequestBody StoreUserSaveParam param) {
        try {
            storeUserManageService.editUser(param);
            return CommonResult.ok("更新成功");
        } catch (Exception e) {
            log.error("编辑用户失败", e);
            return CommonResult.error("更新失败：" + e.getMessage());
        }
    }

    /**
     * 删除用户
     */
    @GetMapping("/delete/{userId}")
    @Operation(summary = "删除用户")
    public CommonResult<String> deleteUser(@PathVariable Long userId) {
        try {
            storeUserManageService.deleteUser(userId);
            return CommonResult.ok("删除成功");
        } catch (Exception e) {
            log.error("删除用户失败", e);
            return CommonResult.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 用户充值申请
     * 保存到user_recharge_review表，等待审核
     */
    @PostMapping("/rechargeApply")
    @Operation(summary = "用户充值申请")
    public CommonResult<String> rechargeApply(@RequestBody StoreUserRechargeApplyParam param) {
        try {
            storeUserManageService.rechargeApply(param);
            return CommonResult.ok("充值申请提交成功，等待审核");
        } catch (Exception e) {
            log.error("用户充值申请失败", e);
            return CommonResult.error("充值申请失败：" + e.getMessage());
        }
    }

    /**
     * 用户充值（直接充值，不需要审核）
     */
    @PostMapping("/recharge")
    @Operation(summary = "用户充值（直接充值）")
    public CommonResult<String> rechargeUser(@RequestBody StoreUserRechargeParam param) {
        try {
            storeUserManageService.rechargeUser(param);
            return CommonResult.ok("充值成功");
        } catch (Exception e) {
            log.error("用户充值失败", e);
            return CommonResult.error("充值失败：" + e.getMessage());
        }
    }

    /**
     * 根据省市区获取业务员列表
     * 对应PHP项目的三级联动获取业务员功能
     */
    @GetMapping("/getSalesmanList")
    @Operation(summary = "根据省市区获取业务员列表")
    public CommonResult<List<StoreUserSalesmanVo>> getSalesmanList(StoreUserSalesmanParam param) {
        try {
            List<StoreUserSalesmanVo> salesmanList = storeUserManageService.getSalesmanList(param);
            return CommonResult.data(salesmanList);
        } catch (Exception e) {
            log.error("获取业务员列表失败", e);
            return CommonResult.error("获取业务员列表失败：" + e.getMessage());
        }
    }
    /**
     * 获取充值记录
     */
    @GetMapping("/getRechargeRecord")
    @Operation(summary = "获取充值记录")
    public CommonResult<Page<BalanceVo>> getRechargeRecord(BalanceBo param) {
        Page page = PageFactory.defaultPage();
        try {
            return CommonResult.data(balanceService.getBalanceLogList(param,page));
        } catch (Exception e) {
            log.error("获取充值记录", e);
            return CommonResult.error("获取充值记录：" + e.getMessage());
        }

    }

    /**
     * 获取用户地址列表
     */
    @GetMapping("/address/index")
    @Operation(summary = "获取用户地址列表")
    public CommonResult<Page<StoreUserAddressListVo>> getUserAddressList(StoreUserAddressListParam param) {
        Page<UserAddress> page = PageFactory.defaultPage();
        try {
            Page<StoreUserAddressListVo> addressList = storeUserManageService.getUserAddressList(page,param);
            return CommonResult.data(addressList);
        } catch (Exception e) {
            log.error("获取用户地址列表失败", e);
            return CommonResult.error("获取用户地址列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取用户地址详情
     */
    @GetMapping("/address/detail/{userId}/{addressId}")
    @Operation(summary = "获取用户地址详情")
    public CommonResult<StoreUserAddressDetailVo> getUserAddressDetail(@PathVariable Long userId, @PathVariable Long addressId) {
        try {
            StoreUserAddressDetailVo addressDetail = storeUserManageService.getUserAddressDetail(userId, addressId);
            return CommonResult.data(addressDetail);
        } catch (Exception e) {
            log.error("获取用户地址详情失败", e);
            return CommonResult.error("获取用户地址详情失败：" + e.getMessage());
        }
    }

    /**
     * 用户地址保存（新增/编辑合一）
     */
    @PostMapping("/address/save")
    @Operation(summary = "用户地址保存(新增/编辑)")
    public CommonResult<String> saveUserAddress(@RequestBody StoreUserAddressSaveParam param) {
        try {
            storeUserManageService.saveUserAddress(param);
            if (param.getAddressId() == null) {
                return CommonResult.ok("新增地址成功");
            } else {
                return CommonResult.ok("编辑地址成功");
            }
        } catch (Exception e) {
            log.error("保存用户地址失败", e);
            return CommonResult.error("保存地址失败：" + e.getMessage());
        }
    }

    /**
     * 删除用户地址
     */
    @GetMapping("/address/delete/{userId}/{addressId}")
    @Operation(summary = "删除用户地址")
    public CommonResult<String> deleteUserAddress(@PathVariable Long userId, @PathVariable Long addressId) {
        try {
            storeUserManageService.deleteUserAddress(userId, addressId);
            return CommonResult.ok("删除地址成功");
        } catch (Exception e) {
            log.error("删除用户地址失败", e);
            return CommonResult.error("删除地址失败：" + e.getMessage());
        }
    }
}
